//
//  WBScrollViewController.m
//  WBImageView
//
//  Created by mac on 16/1/9.
//  Copyright © 2016年 YF_S. All rights reserved.
//

#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height

#import "WBScrollViewController.h"
#import "UIImageView+WebCache.h"

@interface WBScrollViewController ()<UIScrollViewDelegate>{

    UIScrollView *_scrollView;
    
    NSInteger _index;//图片的偏移量
}
@end

@implementation WBScrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
    
    _scrollView.contentSize = CGSizeMake(self.data.count*kScreenW, kScreenH);
    
    //创建View
    for (int i = 0; i < self.data.count; i++) {
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*kScreenW, 0, kScreenW, kScreenH)];
        
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        
        //设置图片
        //将缩略图的URL地址 换成大图的URL地址
        NSString *url = _data[i];
        
        url = [url stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"large"];
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:url]];
        
        //设置响应事件
        imageView.userInteractionEnabled = YES;
        
        //添加手势 ---点击消失
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAct:)];
        
        [imageView addGestureRecognizer:tap];
        
        [_scrollView addSubview:imageView];
    }
    
    //_scrollView的属性
    _scrollView.pagingEnabled = YES;
    
    //开始时隐藏
    _scrollView.hidden = YES;
    
    //设置代理
    _scrollView.delegate = self;
    
    //添加_scrollView
    [self.view addSubview:_scrollView];
}

//点击图片的响应事件
-(void)tapAct:(UITapGestureRecognizer *)tap{

    //获取当前scrollView中imgV对应的WBImageView的Item
    WBImageViewItem *item = self.wbImageView.items[_index];
    
    item.contentMode = UIViewContentModeScaleAspectFit;
    
    //拿到最顶层上来
    [item.superview bringSubviewToFront:item];
    
    //2.坐标系转化为原点
    CGPoint point = [self.view convertPoint:CGPointZero toView:self.wbImageView];
    
    //修改frame
    item.frame = CGRectMake(point.x, point.y, kScreenW, kScreenH);
    
    [self dismissViewControllerAnimated:NO completion:^{
        
        //动画缩小操作
        [UIView animateWithDuration:2 animations:^{
            
            item.frame = item.originFrame;
        } completion:^(BOOL finished) {
            
            item.contentMode = UIViewContentModeScaleToFill;
        }];
    }];
}

//视图即将出现时
-(void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];
    
    //frame的坐标系转化
    CGRect newFrame = [self.item.superview convertRect:self.item.frame toView:self.view];
    
    //重新设置转换后的新坐标
    self.item.frame = newFrame;
    
    self.item.contentMode = UIViewContentModeScaleAspectFit;
    
    //添加到当前的视图
    [self.view addSubview:self.item];
    
    //动画修改frame
    [UIView animateWithDuration:2 animations:^{
        self.item.frame = self.view.frame;
    } completion:^(BOOL finished) {
        
        _scrollView.hidden = NO;
        
        //移除Item
        [self.item removeFromSuperview];
        
        //将Item还原
        self.item.frame = self.item.originFrame;
        
        self.item.contentMode = UIViewContentModeScaleToFill;
        
        [self.wbImageView addSubview:self.item];
        
        //设置_scrollView的偏移
        [_scrollView scrollRectToVisible:CGRectMake(self.item.index*kScreenW, 0, kScreenW, kScreenH) animated:NO];
    }];
}

//_____________UIScrollViewDelegate--------
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    _index = scrollView.contentOffset.x/kScreenW;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
